package com.cimp.util.common;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPClientConfig;
import org.apache.commons.net.ftp.FTPFile;

import java.io.*;
import java.net.SocketException;

/**
 * [简要描述]:
 * [详细描述]:
 *
 * @author huojinhao
 * @date 2018/12/5
 */
public class FTPUtil {
    //private static String encoding = System.getProperty("file.encoding");

    private FTPClient ftpClient;

    public FTPUtil(String url,int port,String username,String password) throws IOException {
        ftpClient = new FTPClient();
        try {
            ftpClient.connect(url,port);
            ftpClient.login(username, password);
            ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
            //ftpClient.setControlEncoding(encoding);
            ftpClient.enterLocalPassiveMode();
            ftpClient.setBufferSize(1024);
            FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_NT);//解决中文乱码
            conf.setServerLanguageCode("zh");
            ftpClient.configure(conf);
        } catch (SocketException e) {
            e.printStackTrace();
            throw new RuntimeException("FTP连接失败！", e);
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("FTP客户端出错！", e);
        }
    }
    public FTPClient connect(String url,int port,String username,String password) {
        ftpClient = new FTPClient();
        try {
            ftpClient.connect(url,port);
            ftpClient.login(username, password);
            ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
            //ftpClient.setControlEncoding(encoding);
            ftpClient.enterLocalPassiveMode();
            ftpClient.setBufferSize(1024);
            FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_NT);
            conf.setServerLanguageCode("zh");
            ftpClient.configure(conf);
        } catch (SocketException e) {
            e.printStackTrace();
            throw new RuntimeException("FTP连接失败！", e);
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("FTP客户端出错！", e);
        }
        return ftpClient;
    }

    public boolean uploadFile(String src,String des) {
        File srcFile = new File(src);
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(srcFile);
            return ftpClient.storeFile(des, fis);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            throw new RuntimeException("文件未找到！", e);
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("FTP客户端出错！", e);
        } finally {
            IOUtils.closeQuietly(fis);
        }
    }

    public void downLoadFile(String remote,String local) {
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(local);
            FTPFile[] files = ftpClient.listFiles();
            System.out.println(files[0].getName());
            if (!ftpClient.retrieveFile(remote, fos)) {
                throw new RuntimeException("FTP文件下载失败");
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            throw new RuntimeException("文件未找到！", e);
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("FTP客户端出错！", e);
        } finally {
            IOUtils.closeQuietly(fos);
        }
    }

    public boolean isDirExists(String name) {
        boolean exist = false;
        if(StringUtils.isBlank(name)) {
            return exist;
        }
        try {
            FTPFile[] files = ftpClient.listDirectories();
            for(FTPFile file : files) {
                if(name.equals(file.getName())) {
                    exist = true;
                    return exist;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("FTP客户端出错！", e);
        }
        return exist;
    }
    public void closeConnect() {
        try {
            ftpClient.disconnect();
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("关闭FTP连接发生异常！", e);
        }
    }

    public void cd(String dir) {
        try {
            ftpClient.changeWorkingDirectory(dir);
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("FTP发生异常！", e);
        }
    }

    public void mkdir(String dir) {
        try {
            ftpClient.makeDirectory(dir);
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("FTP发生异常！", e);
        }
    }
}

